(function(){

    var box=document.getElementById('box');
    var _name=document.getElementById('name');
    var message=document.getElementById('message');
    var btn=document.getElementById('btn');
    render();
    btn.onclick=function(){
        setcookie(_name.value,message.value,1);
        render();
        _name.value='';
        message.value='';
    }
    
    box.onclick=function(event){
        e=event||window.event;
        target=e.target||e.srcElement;
        if(target.nodeName.toUpperCase()=='SPAN')
        {
            target.outerHTML='<input type="text" value="'+target.innerHTML+'"><button class="edit">确认修改</button><button class="del">确认删除</button>';
        }
        if(target.className=='edit')
        {
            var Name=target.parentNode.previousSibling.innerHTML;
            var Value=target.previousSibling.value;
            setcookie(Name,Value,1);
            render();
        }
        if(target.className=='del')
        {
            var Name=target.parentNode.previousSibling.innerHTML;
            removecookie(Name);
            render();
        }
    }

    function render(){
        var html='';
        var arrN=[];
        var arrL=[];
        var arr=document.cookie.split(';');
        if(arr==''){
            html='请输入您的留言信息';
        }else{
            for(var i=0;i<arr.length;i++)
            {
                arrN.push(arr[i].trim());
                arrL.push(arrN[i].split('='));
            }
            for(var j=0;j<arrL.length;j++)
            {
                html+='<li><p>'+arrL[j][0]+'</p><div><span id="span">'+arrL[j][1]+'</span</div></li>';
            }
        }
        box.innerHTML=html;
    }

    function setcookie(name,value,eday) 
    {
        var date=new Date();
        date.setTime(date.getTime()+eday*24*3600*1000);
        date=date.toGMTString();
        document.cookie=name+'='+value+';expires='+date;
    }

    function removecookie(name){
        var date=new Date();
        date.setTime(date.getTime()-1*24*3600*1000);
        date=date.toGMTString();
        document.cookie=name+'=remove;expires='+date;
    }



    /*function getcookie(name){
        var arr=document.cookie.split(';')
        for(var i=0;i<arr.length;i++)
        {
            var str=arr[i].trim();
            if(str.indexOf(name+'=')==0)
            {
                return str.trim().slice((name+'=').length);
            }
        }
        return '';
    }*/

}());
